<template>
  <el-dialog title="设置员工的角色" :visible="isShowDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      <el-button size="small" @click="btnCancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { assignRole } from '@/api/employee'
import { getRoleList } from '@/api/setting'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 当前被点击的员工id
      id: '',
      // 所有角色总表
      list: [],
      // 当前选中的角色id数组
      checkList: []
    }
  },
  async created() {
    const res = await getRoleList({
      page: 1,
      pagesize: 999
    })
    console.log(res)
    this.list = res.rows
  },
  methods: {
    async btnOK() {
      await assignRole({
        id: this.id,
        roleIds: this.checkList
      })
      // 提醒用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('closeDialog')
    },
    btnCancel() {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style>

</style>
